package com.qf.controller.caiwu_controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.*;
import com.qf.service.caiwu_service.*;
import com.qf.utils.caiwu.form.PrefundForm;
import com.qf.utils.caiwu.form.PrefundOrderForm;
import com.qf.utils.caiwu.form.PrefundReportForm;
import com.qf.utils.caiwu.vo.PrefundReportVO;
import com.qf.utils.caiwu.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/12 0012
 * Author->DT
 * sassspringboot
 * 行到水穷处 坐看云起时
 * 模块：
 */
@Controller
@RequestMapping("/prefund")
public class PrefundController {
    @Autowired
    private PrefundService prefundService;
    @Autowired
    private ClientTypeService clientTypeService;
    @Autowired
    private ClientNameService clientNameService;
    @Autowired
    private PrefundRuleService prefundRuleService;
    @Autowired
    private PrefundOrderService prefundOrderService;
    //跳转展示预存款信息页面
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer index,
                       Model model, PrefundForm form){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("form", form);
        //分页查询欠款信息列表
        PageInfo<Prefund> pageInfo = prefundService.findPrefundListByLimit(map);
        //查询全部客户类型
        List<ClientType> clientTypeList=clientTypeService.findAllClientType();
        //判断form中的nid是否为null
        List<ClientName> nameList=null;
        if (form.getNid()!=null){
            //nid不为null,要查询名称列表进行二级联动的查询条件回显
            nameList = clientNameService.selectAll(form.getTid());
        }
        //将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("clientTypeList", clientTypeList);
        //数据回显信息放入Request域中
        model.addAttribute("form", form);
        model.addAttribute("nameList", nameList);
        return "/caiwu/prefund-list";
    }

    //跳转到增加预存款页面
    @RequestMapping("/add-ui")
    public String addUI(Model model){
        //查询全部客户类型
        List<ClientType> clientTypeList=clientTypeService.findAllClientType();
        //将查询到的信息放入Request域中
        model.addAttribute("clientTypeList", clientTypeList);
        //返回
        return "/caiwu/prefund-add";
    }

    //新增预存款信息
    @RequestMapping("/add")
    @ResponseBody
    public ResultVO add(Prefund prefund){
        int count = prefundService.insertSelective(prefund);
        //返回
        if (count==1){
            return new ResultVO(0, "预存款信息添加成功！", null);
        }
        return new ResultVO(1, "预存款信息添加失败！", null);
    }

    //跳转到增加预存款订单页面
    @RequestMapping("/order/add-ui")
    public String orderAddUI(Model model){
        //查询当前预存款表中全部客户
        List<Prefund> clientList=prefundService.findAllClient();
        //查询全部预存款规则信息
        List<PrefundRule> ruleList = prefundRuleService.findAll();
        //将查询到的信息放入Request域中
        model.addAttribute("clientList", clientList);
        model.addAttribute("ruleList", ruleList);
        //返回
        return "/caiwu/prefund-order-add";
    }

    //新增预存款订单信息
    @RequestMapping("/order/add")
    @ResponseBody
    public ResultVO orderAdd(PrefundOrder prefundOrder){
        //执行增加
        int count = prefundOrderService.insertSelective(prefundOrder);
        //返回
        if (count==1){
            return new ResultVO(0, "预存款订单信息添加成功！", null);
        }
        return new ResultVO(1, "预存款订单信息添加失败！", null);
    }

    //跳转展示预存款使用记录页面
    @RequestMapping("/order/list")
    public String orderList(@RequestParam(defaultValue = "1")Integer index,
                       Model model, PrefundOrderForm form){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("form", form);
        //分页查询预存款使用记录列表
        PageInfo<PrefundOrder> pageInfo = prefundOrderService.findPrefundOrderListByLimit(map);
        //将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        //数据回显信息放入Request域中
        model.addAttribute("form", form);
        return "/caiwu/prefund-order-list";
    }

    //跳转客户预存款报表页面
    @RequestMapping("/report")
    public String report(@RequestParam(defaultValue = "1")Integer index,
                            Model model, PrefundReportForm form){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("form", form);
        //查询当前预存款表中全部客户
        List<Prefund> clientList=prefundService.findAllClient();
        //分页查询预存款使用记录列表
        PageInfo<PrefundReportVO> pageInfo = prefundService.findPrefundReportByLimit(map);
        //将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("clientList", clientList);
        //数据回显信息放入Request域中
        model.addAttribute("form", form);
        return "/caiwu/prefund-report";
    }
}
